package controller.sistema_veiculos;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Veiculo;

import persistencia.PersistenciaVeiculo;

@WebServlet("/ServletConsultaVeiculo")
public class ServletConsultaVeiculo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Veiculo veiculo = requestToVeiculo(request);
		PersistenciaVeiculo pv = new PersistenciaVeiculo();
		ArrayList<Veiculo> veiculos = pv.getVeiculos(veiculo);
		HttpSession session = request.getSession();
		Veiculo veiculo2 = (Veiculo) session.getAttribute("veiculo");
		System.out.println(veiculo2.getChassi());
		session.setAttribute("veiculos", veiculos);
		RequestDispatcher rd = request.getRequestDispatcher("/sistema_veiculos/consultaVeiculo.jsp");
		rd.forward(request, response);
	}

	private Veiculo requestToVeiculo(HttpServletRequest request) {
		Veiculo veiculo = new Veiculo();
		String chassi = request.getParameter("chassi");
		veiculo.setChassi(chassi);		
		return veiculo;
	}
}